// 导入组件
import Vue from 'vue';
import Router from 'vue-router';
// 登录
import login from '@/views/login';
// 首页
import index from '@/views/index';
/**
 * 反馈管理
 */
// 反馈列表
import FeedbackList from '@/views/feedback/feedbackList';
// 技术反馈列表
import TechnicalFeedbackeedback from '@/views/feedback/technicalFeedbackeedback';
// 反馈详情
import FeedbackDetail from '@/views/feedback/feedbackDetail';
// 常见问题
import ProblemList from '@/views/feedback/problemList';
// 模块列表
import ModuleList from '@/views/feedback/moduleList';
// 问题日志
import ProblemLog from '@/views/feedback/problemLog';
// 标签设置
import LabelSetting from '@/views/feedback/labelSetting';
// 版本管理
import VersionManagement from '@/views/version/versionManagement';

/**
 * 系统管理
 */
// 隐私政策
import PrivacyPolicy from '@/views/system/privacyPolicy';
// 注册协议
import RegistrationAgreement from '@/views/system/registrationAgreement';

// 用户管理
import user from '@/views/system/user';
// 菜单管理
import Module from '@/views/system/Module';
// 角色管理
import Role from '@/views/system/Role';
// 公司管理
import Dept from '@/views/system/Dept';
// 权限管理
import Permission from '@/views/system/Permission';


import * as dd from 'dingtalk-jsapi' // 此方式为整体加载，也可按需进行加载
import {
  getCode
} from '@/utils/dd' // dd获取免登授权码

// 自动生成页面--自动添加路由
import addRoute from '@/router/addRoute.js'


// 启用路由
Vue.use(Router);
// 路由列表
let routes = [
  {
    path: '/',
    name: '',
    component: login,
    hidden: true,
    meta: {
      // requireAuth: false
      requireAuth: true
    }
  },
  {
    path: '/login',
    name: '登录',
    component: login,
    hidden: true,
    meta: {
      requireAuth: false
    }
  }, {
  path: '/index',
  name: '首页',
  component: index,
  iconCls: 'el-icon-tickets',
  children: [{
    path: '/feedback/feedbackList',
    name: '反馈列表',
    component: FeedbackList,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/feedback/technicalFeedbackeedback',
    name: '技术反馈列表',
    component: TechnicalFeedbackeedback,
    meta: {
      requireAuth: true
    }
  },{
    path: '/feedback/feedbackDetail',
    name: '详情',
    component: FeedbackDetail,
    meta: {
      requireAuth: true
    }
  },{
    path: '/feedback/problemList',
    name: '常见问题',
    component: ProblemList,
    meta: {
      requireAuth: true
    }
  },{
    path: '/feedback/problemLog',
    name: '常见问题',
    component: ProblemLog,
    meta: {
      requireAuth: true
    }
  },{
    path: '/feedback/moduleList',
    name: '模块列表',
    component: ModuleList,
    meta: {
      requireAuth: true
    }
  },{
    path: '/feedback/labelSetting',
    name: '标签设置',
    component: LabelSetting,
    meta: {
      requireAuth: true
    }
  },{
    path: '/system/privacyPolicy',
    name: '隐私政策',
    component: PrivacyPolicy,
    meta: {
      requireAuth: true
    }
  },{
    path: '/system/registrationAgreement',
    name: '注册协议',
    component: RegistrationAgreement,
    meta: {
      requireAuth: true
    }
  },{
    path: '/system/user',
    name: '用户管理',
    component: user,
    meta: {
      requireAuth: true
    }
  }, {
    path: '/system/Module',
    name: '菜单管理',
    component: Module,
    meta: {
      requireAuth: true
    }
  }, {
    path: '/system/Role',
    name: '角色管理',
    component: Role,
    meta: {
      requireAuth: true
    }
  }, {
    path: '/system/Dept',
    name: '公司管理',
    component: Dept,
    meta: {
      requireAuth: true
    }
  }, {
    path: '/system/Permission',
    name: '权限管理',
    component: Permission,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/version/versionManagement',
    name: '版本管理',
    component: VersionManagement,
    meta: {
      requireAuth: true
    }
  }
  ]
}]


// 自动生成页面--自动添加路由
if(addRoute&&addRoute.length>0){
  routes = routes.concat(addRoute)
  // routes = routes.map((item)=>{
  //   if(item.path == '/index'){
  //     item.children = item.children.concat(addRoute)
  //   }
  //   return {
  //     ...item
  //   }
  // })
}
// console.log('addRoute', addRoute)
// console.log('routes', routes)

// 导出路由
let router = new Router({
  routes: routes
})

// const originalPush = Router.prototype.push
// Router.prototype.push = function push(location, onResolve, onReject) {
//   if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
//   return originalPush.call(this, location).catch((err) => err)
// }

// 路由拦截器
router.beforeEach((to, from, next) => {
  if (to.matched.length != 0) {
    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
      if (Boolean(localStorage.getItem("dingToken"))) { // 通过vuex state获取当前的user是否存在
        if (to.path != "/" && to.path != "/login") { //判断是否要跳到登录界面
          next();
        } else {
          /**
           * 防刷新，如果登录，修改路由跳转到登录页面，修改路由为登录后的首页
           */
          next({
            path: '/feedback/feedbackList'
          })
        }
      } else {
        console.log('dd.env.platform', dd.env.platform)
        if (dd.env.platform !== 'notInDingTalk') {
          getCode()
        } else {
          // 否则全部重定向到登录页
          next({
            path: '/login',
            query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
          })
        }

      }
    } else {
      if (Boolean(localStorage.getItem("dingToken"))) { // 判断是否登录
        if (to.path != "/" && to.path != "/login") { //判断是否要跳到登录界面
          next();
        } else {
          /**
           * 防刷新，如果登录，修改路由跳转到登录页面，修改路由为登录后的首页
           */
          next({
            path: '/feedback/feedbackList'
          })
        }
      } else {
        next();
      }
    }
  } else {
    next({
      path: '/login',
      query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
    })
  }
})

export default router
